<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { PureTableBar } from "@/components/RePureTableBar";
import { ref } from "vue";
import tree from "./tree.vue";
import { useUser } from "./utils/hook";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "SystemMenu"
});

const treeRef = ref();
const formRef = ref();
const tableRef = ref();
const {
  loading,
  columns,
  dataList,
  treeData,
  treeLoading,
  currentTreeData,
  pagination,
  deviceDetection,
  tabType,
  onSearch,
  loadTree,
  openDialog,
  onTreeSelect,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useUser(tableRef, treeRef);
</script>

<template>
  <div>
    <!-- <el-tabs v-model="tabType" type="border-card" class="menu-tabs">
      <el-tab-pane label="平台菜单" :name="1" />
      <el-tab-pane label="门店菜单" :name="3" />
    </el-tabs> -->
    <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
      <tree
        ref="treeRef"
        :class="['mr-[10px]', deviceDetection() ? 'w-full' : 'min-w-[280px]']"
        :tabType="tabType"
        :treeData="treeData"
        :treeLoading="treeLoading"
        @tree-select="onTreeSelect"
        @load-tree="loadTree"
      />
      <div
        :class="[
          deviceDetection() ? ['w-full', 'mt-2'] : 'w-[calc(100%-290px)]'
        ]"
      >
        <!-- <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="按钮名称：" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form> -->
        <PureTableBar
          title=""
          :columns="columns"
          class="!mt-0"
          @refresh="onSearch"
        >
          <template #buttons>
            <el-button
              type="primary"
              :icon="useRenderIcon(AddFill)"
              :disabled="!currentTreeData.isLeaf"
              @click="openDialog('新增', 2)"
            >
              新增按钮
            </el-button>
          </template>
          <template v-slot="{ size, dynamicColumns }">
            <pure-table
              ref="tableRef"
              row-key="id"
              adaptive
              :adaptiveConfig="{ offsetBottom: 90 }"
              align-whole="center"
              :loading="loading"
              :size="size"
              :data="dataList"
              :columns="dynamicColumns"
              :pagination="{ ...pagination, size }"
              :header-cell-style="{
                background: 'var(--el-fill-color-light)',
                color: 'var(--el-text-color-primary)'
              }"
              @selection-change="handleSelectionChange"
              @page-size-change="handleSizeChange"
              @page-current-change="handleCurrentChange"
            >
              <template #operation="{ row }">
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(EditPen)"
                  @click="openDialog('修改', 2, row)"
                >
                  修改
                </el-button>
                <el-popconfirm
                  :title="`是否确认删除按钮: '${row.menuName}' ?`"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin"
                      link
                      type="danger"
                      :size="size"
                      :icon="useRenderIcon(Delete)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </template>
            </pure-table>
          </template>
        </PureTableBar>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.menu-tabs {
  border: none;
}
:deep(.menu-tabs .el-tabs__item) {
  padding: 0 20px !important;
}
:deep(.menu-tabs .el-tabs__content) {
  padding: 0;
}
</style>
